package com.wobangkj.common.service.impl;

import com.wobangkj.common.model.crud.Share;
import com.wobangkj.common.service.ShareService;
import com.wobangkj.tool.api.result.Result;
import com.wobangkj.tool.util.sql.CrudUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wobangkj.common.repository.ShareRepository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

import static com.wobangkj.tool.util.RestUtil.copyNonNullProperties;

@Service
@Slf4j
public class ShareServiceImpl implements ShareService{
    @Autowired
    private ShareRepository repository;

    @Override
    public Object search(Map<String, Object> params, Share data) {

        return CrudUtil.search(params, data, repository);
    }

    @Override
    public Object id(Long id) {
        return CrudUtil.getById(id, repository);
    }

    @Override
    public Object delete(Long id) {
        return CrudUtil.delete(id, repository);
    }
    @Override
    public Object update(Share share) {

        Share existData = repository.findById(share.getId()).get();
        copyNonNullProperties(share, existData);
        return CrudUtil.update(existData, repository);
    }

    @Override
    @SuppressWarnings("Duplicates")
    public Object create(Share share) {
        try {
            Share createData = repository.save(share);
            // 使用 uuid 作为源 token
            //String     token = UUID.randomUUID().toString().replace("-", "");
            //TokenModel model = new TokenModel(createData.getId(), token);
            // 30 分钟有效期
            //cacheManager.set(model.getToken(), new CacheModel(TOKEN_MINUTE, model));
            return Result.GetMapData(Result.CodeCreate, Result.MsgCreate);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }

    @Override
    public List<Share> findByNameLike(String share_title){return repository.findByNameLike(share_title);}

    @Override
    public List<Share> selectAllShareView(){return repository.selectAllShareView();};

    @Override
    public List<Map> list(){return repository.list();};

    @Override
    public List<Map> list(Integer share_class){return repository.list(share_class);}

    @Override
    public List<Map> idlist(Long id) {
        return repository.idlist(id);
    }

    ;

    @Override
    public List<Map> list(String share_company,Integer college,Integer major,String share_job){return repository.list(share_company,college,major,share_job);}

    @Transactional
    @Override
    public Object updateShareForward(Long id) {

        try {
            repository.updateShareForward(id);
            return Result.GetMapData(Result.CodeSuccess,Result.MsgSuccess);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }

    @Transactional
    @Override
    public Object updateUrl(String url,String id) {

        try {
            repository.updateUrl(url,id);
            return Result.GetMapData(Result.CodeSuccess,Result.MsgSuccess);//, model);
        } catch (Exception e) {
            return Result.GetMapData(Result.CodeSql, e.getCause().getCause().getMessage());
        }
    }


}
